package cn.simon.service;

import cn.simon.mapper.GoodsClassifyMapper;

import cn.simon.mapper.GoodsClassifySecMapper;
import cn.simon.model.GoodsClassify;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import java.util.List;

/**
 * @author Kinfer
 * @date 2022/10/24
 */
@Log
@Service
public class GoodsClassifyService {

    // 一级分类状态：删除
    private static final Integer STATE_ZERO = 0;
    // 一级分类状态：发布
    private static final Integer STATE_ONE = 1;

    @Autowired
    private GoodsClassifyMapper goodsClassifyMapper;

    @Autowired
    private GoodsClassifySecMapper goodsClassifySecMapper;

    /**
     * 查询所有商品一级分类
     *
     * @return goodsClassifyList
     * @author Kinfer
     * @date 2022/10/24
     */
    public List<GoodsClassify> getAllGoodsClassify() {
        QueryWrapper<GoodsClassify> wrapper = new QueryWrapper<>();
        wrapper.eq("classify_state", STATE_ONE);

        List<GoodsClassify> goodsClassifyList = goodsClassifyMapper.selectList(wrapper);
        return goodsClassifyList;
    }

    public int addClassify(GoodsClassify goodsClassify) {
        return goodsClassifyMapper.insertById(goodsClassify);
    }

    public GoodsClassify findById(Integer id) {
        GoodsClassify goodsClassify = goodsClassifyMapper.selectById(id);
        return goodsClassify;
    }

    public int updateClassify(GoodsClassify goodsClassify) {
        return goodsClassifyMapper.updateById(goodsClassify);
    }

    @Transactional
    public int deleteClassifyById(Integer id) {
        goodsClassifySecMapper.deleteByClassifyId(id);
        goodsClassifyMapper.deleteById(id);
        return 1;
    }


}
